package com.rajaev.dtu.tractor.interfaces;

import com.github.pagehelper.PageInfo;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import com.rajaev.dtu.tractor.app.service.BatteryService;
import com.rajaev.dtu.tractor.app.service.UserService;
import com.rajaev.dtu.tractor.domain.entity.UserInfo;
import com.rajaev.dtu.tractor.domain.entity.UserPo;
import com.rajaev.dtu.tractor.domain.vo.BatteryBasicVo;
import com.rajaev.dtu.tractor.domain.vo.BatteryCheckStaticVo;
import com.rajaev.dtu.tractor.domain.vo.BatteryDailyRuntimeVo;
import com.rajaev.dtu.tractor.domain.vo.BatteryQueryVo;
import com.rajaev.dtu.tractor.infrastructure.base.common.Constants;
import com.rajaev.dtu.tractor.infrastructure.base.errors.BusinessException;
import com.rajaev.dtu.tractor.infrastructure.helper.MongoDbUtils;
import com.rajaev.dtu.tractor.infrastructure.helper.UserSecurityUtil;
import com.rajaev.dtu.tractor.interfaces.dto.LoginUserWrapper;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.mongodb.client.model.Filters.eq;

@RestController
public class BatteryController {

    @Autowired
    BatteryService batteryService;

    @PostMapping("/save")
    public void addBattery(String vin){
        batteryService.save(vin);
    }

    @Autowired
    UserSecurityUtil userSecurityUtil;


    @Autowired
    MongoDbUtils mongoDbUtils;

    @Autowired
    UserService userService;


    /**
     * 标记电池合格
     */
    @PostMapping("/battery/setBatteryQualified")
    public void checkBatteryOk(Integer batteryId) throws BusinessException {
        batteryService.checkBatteryOk(batteryId);
    }

    @GetMapping("/battery/selectBatteryDailyRuntimeVo")
    public BatteryDailyRuntimeVo selectBatteryDailyRuntimeVo(Integer batteryId, Integer startDate, Integer endDate){
        return batteryService.selectBatteryDailyRuntimeVo(batteryId, startDate, endDate);
    }



    @GetMapping("/battery/getList")
    public PageInfo<BatteryBasicVo> pageQueryBattery(HttpServletRequest request, BatteryQueryVo batteryQueryVo) {
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();

        List<Integer> customerIds = userService.selectUserCustomerIds(userInfo.getId());
        batteryQueryVo.setCustomerIds(customerIds);
        return batteryService.pageQueryBattery(batteryQueryVo);
    }

    @PostMapping("battery/edit")
    public void editBattery(String bz, String plateNo,Integer projectTeamId,  Integer batteryId, String deviceManager) {
        batteryService.editBatteryInfo(batteryId,projectTeamId, bz, plateNo, deviceManager);
    }

    @GetMapping("battery/queryDeviceByLast4Vin")
    public List<String> queryDeviceByLast4Vin(String vin){
        return batteryService.queryVinList(vin);
    }




    @GetMapping("/battery/optLog")
    public List BatteryLog(Integer batterId) {
        List list = new ArrayList();
        FindIterable findIterable = mongoDbUtils.getDatabase().getCollection(Constants.batteryOptHistoryCollectionName)
                .find(eq("batteryId", batterId))
                .sort(new BasicDBObject("logTime", -1));
        MongoCursor cursor = findIterable.iterator();

        while (cursor.hasNext()) {
            Document document = (Document) cursor.next();
            Map map = new HashMap<String, String>(1<<3);
            document.entrySet().stream().forEach(kv -> {
                if (!kv.getKey().equalsIgnoreCase("_id")) {
                    map.put(kv.getKey(), kv.getValue());
                }
            });
            list.add(map);
        }
        return list;
    }
}
